class AListener {
  constructor() {
    this._pool = new Map();
  }

  once(eventName, callback) {
    const callbackList = this._pool.get(eventName);
    if ( !callbackList ){
      this._pool.set(eventName, new Array())
    }
    this._pool.get(eventName).push(callback)
  }

  emit(eventName, commit) {
    const callbackList = this._pool.get(eventName);
    if (!callbackList) {
      return
    }

    for(const callback of callbackList){
      Promise.resolve().then(() => callback(commit));
    }
    this.removeAll(eventName);
  }

  // 删除所有的监听器
  removeAll(eventName) {
    this._pool.delete(eventName)
  }
}

export default new AListener();
